﻿#include "BaseKit.h"
#include "platform/CAFileUtils.h"

string authorization="";
unsigned int userId=0;
string nickName="";
CAColor4B themeColor=ccc4(59, 180, 112, 255);
//项目根路径
char baseUrl[]="http://127.0.0.1:8000";
//字体大小和颜色
CAFont font;
//距离顶部的高度
float mt=0.0f;

/**
 * 发送http请求
 */
void sendHttp(CAView* view, const char* url, const std::string& parameters, const StringCallback& callback, const CAHttpRequest::Type httpType, const bool json, const bool createAnimation){
	if(createAnimation){
		CAView* subview=CAView::createWithLayout(DLayoutFill);
		subview->setAlpha(0);
		subview->setTag(-100);
		subview->setZOrder(9999);
		CAImageView* imageView=CAImageView::createWithLayout(DLayout(DHorizontalLayout_W_C(8.2, 0.5), DVerticalLayout_H_C(7.5, 0.5)));
		imageView->setImage(CAImage::create("img/loading.png"));
		imageView->setScale(1);
		CAViewAnimation::beginAnimations("loading");
		CAViewAnimation::setAnimationDuration(0.3f);
		CAViewAnimation::setAnimationRepeatCount(99999999999);
		CAViewAnimation::setAnimationRepeatAutoreverses(true);
		imageView->setScale(30);
		CAViewAnimation::commitAnimations();
		subview->addSubview(imageView);
		
		view->addSubview(subview);
	}
	std::vector<std::string> headers;
	headers.push_back(authorization);
	if(json){
		headers.push_back("Content-Type: application/json; charset=utf-8");
	}else{
		//headers.push_back("Content-Type: multipart/form-data; charset=utf-8");
	}
	string urls=obj2str("%s%s", baseUrl, url);
	CAHttpRequest* http=CAHttpRequest::create();
	if(httpType==CAHttpRequest::Type::Get){
		urls.append("?");
		urls.append(parameters);
		http->setUrl(urls);
	}else{
		http->setUrl(urls);
		http->setRequestData(parameters.c_str(), strlen(parameters.c_str()));
	}
	http->setHeaders(headers);
	http->setRequestType(httpType);
	http->setResponseCallback([=](CAHttpClient* client, CAHttpResponse* res){
		if(res->isSucceed()){
			callback(res->getResponseData()->toString());
			if(createAnimation==true&&view!=nullptr&&view->getSubviewByTag(-100)!=nullptr){
				CAViewAnimation::removeAnimations("loading");
				view->removeSubviewByTag(-100);
			}
			
			return;
		}

		if(createAnimation==true&&view!=nullptr&&view->getSubviewByTag(-100)!=nullptr){
			CAViewAnimation::removeAnimations("loading");
			view->removeSubviewByTag(-100);
			getAlert(view, "服务器异常, 请稍后重试");
		}
	});
	CAHttpClient::getInstance(1)->send(http);
}

/**
* 获得根窗口
*/
CANavigationController* getRootWindow(){
	mt=-90.0f;
	return RootWindow::getInstance()->getRoot();
}

/**
* 获得一个子视图,用于一行多列的情况
*/
CAView* getSubview(){
	mt+=100;
	return CAView::createWithLayout(DLayout(DHorizontalLayoutFill, DVerticalLayout_T_H(mt, 90)));
}

/**
* 获得一个显示文本
*/
CALabel* getLabel(const string& text){
	CALabel* label=CALabel::createWithLayout(DLayout(DHorizontalLayout_L_W(0, 130), DVerticalLayout_T_H(15, 60)));
	label->setText(text);
	label->setFont(font);
	label->setVerticalTextAlignmet(CAVerticalTextAlignment::Center);
	label->setTextAlignment(CATextAlignment::Right);

	return label;
}

/**
* 获得一个输入框
*/
CATextField* getTextField(){
	return CATextField::createWithLayout(DLayout(DHorizontalLayout_L_R(140, 10), DVerticalLayout_T_H(0, 90)));
}

/**
* 获得一个弹窗提示
*/
void getAlert(CAView* view, const string& message){
	CAView* subview=CAView::createWithLayout(DLayout(DHorizontalLayout_W_C(500, 0.5), DVerticalLayout_H_C(120, 0.5)), ccc4(66, 66, 78, 255));
	subview->setTag(-200);
	subview->setZOrder(100);
	CALabel* txt=CALabel::createWithLayout(DLayout(DHorizontalLayout_W_C(500, 0.5), DVerticalLayout_H_C(120, 0.5)));
	txt->setText(message);
	txt->setFontSize(40);
	txt->setColor(CAColor4B::WHITE);
	txt->setTextAlignment(CATextAlignment::Center);
	txt->setVerticalTextAlignmet(CAVerticalTextAlignment::Center);
	subview->addSubview(txt);
	view->insertSubview(subview, 100);
	CAScheduler::getScheduler()->scheduleOnce([=](float interval){
		view->removeSubviewByTag(-200);
	}, "alert_label", view, 3);
}

/**
* 获得一个选择器
*/
CAPickerView* getPicker(CAView* view){
	CAView* subview=CAView::createWithLayout(DLayout(DHorizontalLayoutFill, DVerticalLayout_B_H(0, 500)));
	subview->setColor(ccc4(190, 190, 190, 255));
	subview->setZOrder(50);
	subview->setTag(-300);
	view->addSubview(subview);
	CAButton* btn=CAButton::createWithLayout(DLayout(DHorizontalLayout_R_W(0, 120), DVerticalLayout_T_H(0, 100)), CAButton::Type::Custom);
	btn->setTitleForState(CAControl::State::Normal, "确定");
	btn->setTitleFontSize(60);
	btn->setTitleColorForState(CAControl::State::Normal, themeColor);
	btn->addTarget([=]{
		subview->removeAllSubviews();
		view->removeSubview(subview);
	}, CAButton::Event::TouchUpInSide);
	subview->addSubview(btn);
	CAPickerView* pickerView=CAPickerView::createWithLayout(DLayout(DHorizontalLayoutFill, DVerticalLayout_T_H(100, 400)));
	pickerView->setFontSizeNormal(70);
	pickerView->setBackgroundImage(CAImage::createWithColor4B(ccc4(190, 190, 190, 255)));
	pickerView->onHeightForComponent([](unsigned int component){
		return 80;
	});
	pickerView->onViewForSelected([&](unsigned int component, DSize size){
		return CAView::createWithColor(themeColor);
	});
	subview->addSubview(pickerView);
	
	return pickerView;
}

/**
* 获得一个日期选择器
*/
CADatePickerView* getDatePicker(CAView* view, const CADatePickerView::Mode mode){
	CAView* subview=CAView::createWithLayout(DLayout(DHorizontalLayoutFill, DVerticalLayout_B_H(0, 500)));
	subview->setColor(ccc4(190, 190, 190, 255));
	view->addSubview(subview);
	CAButton* btn=CAButton::createWithLayout(DLayout(DHorizontalLayout_R_W(0, 120), DVerticalLayout_T_H(0, 100)), CAButton::Type::Custom);
	btn->setTitleForState(CAControl::State::Normal, "确定");
	btn->setTitleFontSize(60);
	btn->setTitleColorForState(CAControl::State::Normal, themeColor);
	btn->addTarget([=]{
		subview->removeAllSubviews();
		view->removeSubview(subview);
	}, CAButton::Event::TouchDown);
	subview->addSubview(btn);
	CADatePickerView* pickerView=CADatePickerView::createWithLayout(DLayout(DHorizontalLayoutFill, DVerticalLayout_T_H(100, 400)), mode);
	pickerView->setBackgroundImage(CAImage::createWithColor4B(ccc4(190, 190, 190, 255)));
	pickerView->setBackgroundImageForSelected(CAImage::createWithColor4B(themeColor));
	subview->addSubview(pickerView);

	return pickerView;
}

/**
 * 单例模式创建CAWebView
 */
static std::map<int, CAWebView*> webClient;
CAWebView* getWebClient(int threadId, float top, float height){
	if(webClient.find(threadId)==webClient.end()){
		CAWebView* web=CAWebView::createWithLayout(DLayout(DHorizontalLayoutFill, DVerticalLayout_T_H(top, height)));
		web->setVisible(false);
		web->showLoadingActivity(false);
		web->setJavascriptInterfaceScheme("j2c");
		webClient[threadId]=web;
	}

	return webClient.at(threadId);
}
